package com.tuomaxu.pojo;

import com.tuomaxu.utils.HttpProtocolUtil;
import com.tuomaxu.utils.StaticResourceUtil;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class Response {
    private OutputStream outputStream;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void write(String absolutePath) throws IOException {
        File file = new File(absolutePath);
        if(file.exists() && file.isFile()){
            StaticResourceUtil.outputStaticResource(file,outputStream);
        }else {
            outputStream.write(HttpProtocolUtil.getHttp404().getBytes(StandardCharsets.UTF_8));
        }
    }
}
